package org.snaker.engine.parser.xml;

import org.snaker.engine.helper.XmlHelper;
import org.snaker.engine.model.TransitionModel;
import org.snaker.engine.parser.AbstractNodeParser;
import org.w3c.dom.Element;

import java.util.List;

public abstract class XMLAbstractNodeParser extends AbstractNodeParser<Element> {

    /**
     * 实现NodeParser接口的parse函数
     * 由子类产生各自的模型对象，设置常用的名称属性，并且解析子节点transition，构造TransitionModel模型对象
     */
    public void parse(Element element) {
        nodeModel = newModel();
        nodeModel.setName(element.getAttribute(ATTR_NAME));
        nodeModel.setDisplayName(element.getAttribute(ATTR_DISPLAYNAME));
        nodeModel.setLayout(element.getAttribute(ATTR_LAYOUT));
        nodeModel.setPreInterceptors(element.getAttribute(ATTR_PREINTERCEPTORS));
        nodeModel.setPostInterceptors(element.getAttribute(ATTR_POSTINTERCEPTORS));

        List<Element> transitions = XmlHelper.elements(element, NODE_TRANSITION);
        for(Element te : transitions) {
            TransitionModel transition = new TransitionModel();
            transition.setName(te.getAttribute(ATTR_NAME));
            transition.setDisplayName(te.getAttribute(ATTR_DISPLAYNAME));
            transition.setTo(te.getAttribute(ATTR_TO));
            transition.setExpr(te.getAttribute(ATTR_EXPR));
            transition.setG(te.getAttribute(ATTR_G));
            transition.setOffset(te.getAttribute(ATTR_OFFSET));
            transition.setSource(nodeModel);
            nodeModel.getOutputs().add(transition);
        }

        parseNode(nodeModel, element);
    }
}
